package com.tree.common.config.swagger;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

/**
 * @ClassName com.demo.common.swagger
 * Description: <类功能描述>. <br>
 * <p>
 * <使用说明>
 *    http://springfox.github.io/springfox/docs/current/#springfox-spring-mvc-and-spring-boot
 * </p>
 * @Author tree
 * @Date 2019-05-25 21:27
 * @Version 1.0
 */
@EnableSwagger2
@Configuration
@ConditionalOnClass({Docket.class})
@ConditionalOnProperty(value = {"swagger.enable"},matchIfMissing = true)
@EnableConfigurationProperties({SwaggerProperties.class})
public class SwaggerConfig {

    @Bean
    @DependsOn({"swaggerApiInfo"})
    public Docket createRestApi(SwaggerProperties swaggerProperties, ApiInfo apiInfo){
        return (new Docket(DocumentationType.SWAGGER_2)).apiInfo(apiInfo).select()
                .apis(RequestHandlerSelectors.basePackage("com.demo"))
                .paths(PathSelectors.any()).build();
    }

    @Bean({"swaggerApiInfo"})
    public ApiInfo apiInfo(SwaggerProperties swaggerProperties ){
        return (new ApiInfoBuilder()).title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription()).termsOfServiceUrl("")
                .version(swaggerProperties.getVersion()).build();
    }

}
